package com.htt.app.babau.sqlite;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.htt.app.babau.objects.Details;
import com.htt.app.babau.utils.Log;

public class DAODetails {
	 
    protected static final String TAG = "DAODetails"; 
    
	private String[] allColumns = { MyDataBaseHelper.DetailID,
		      MyDataBaseHelper.DetailCategoriesId,	
		      MyDataBaseHelper.DetailTitle,
		      MyDataBaseHelper.DetailContent,
		      MyDataBaseHelper.DetailImage };
	
    private Context context; 
    private SQLiteDatabase db; 
    private MyDataBaseHelper dbHelper; 
 
    public DAODetails(Context context)  
    { 
        this.context = context; 
        dbHelper = new MyDataBaseHelper(context); 
    } 
 
    public DAODetails createDatabase() throws SQLException  
    { 
    	synchronized (MyDataBaseHelper.Lock) {
    		try  
            { 
                dbHelper.createDataBase(); 
            }  
            catch (IOException mIOException)  
            { 
                Log.e(TAG, mIOException.toString() + "  UnableToCreateDatabase"); 
            } 
            return this; 
		}
        
    } 
 
    public DAODetails open() throws SQLException  
    { 
    	synchronized (MyDataBaseHelper.Lock) {
    		try  
            { 
                dbHelper.openDataBase(); 
                dbHelper.close(); 
                db = dbHelper.getReadableDatabase(); 
            }  
            catch (SQLException mSQLException)  
            { 
                Log.e(TAG, "open >>"+ mSQLException.toString()); 
            } 
            return this; 
		}
        
    } 
 
    public void close()  
    { 
    	if(db != null)
        db.close(); 
    } 
 
 // Adding
    public void insertDetail(Details item) {
    	try {
	        ContentValues values = new ContentValues();
	        values.put(MyDataBaseHelper.DetailID, item.getId()); 
	        values.put(MyDataBaseHelper.DetailCategoriesId, item.getCategories_id());
	        values.put(MyDataBaseHelper.DetailTitle, item.getTitle());
	        values.put(MyDataBaseHelper.DetailContent, item.getContent());
	        values.put(MyDataBaseHelper.DetailImage, item.getImage());
	        
	        // Inserting Row
	        if(db.insert(MyDataBaseHelper.Details, null, values)!=-1)
	        	Log.w(TAG, "insert sucess");
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }
    
    // delete
    public void deleteDetail(Details item) {
    	try {
    		db.delete(MyDataBaseHelper.Details, MyDataBaseHelper.DetailID + " = " + item.getId(), null);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }
    
    public void deleteAllDetail() {
    	try {
	    	int doneDelete = 0;
	        doneDelete = db.delete(MyDataBaseHelper.Details, null, null);
	        Log.w(TAG, Integer.toString(doneDelete));
	    } catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    // update
    public void updateDetail(Details item) {
    	try {
	    	ContentValues values = new ContentValues();
	    	values.put(MyDataBaseHelper.DetailID, item.getId()); 
	        values.put(MyDataBaseHelper.DetailCategoriesId, item.getCategories_id());
	        values.put(MyDataBaseHelper.DetailTitle, item.getTitle());
	        values.put(MyDataBaseHelper.DetailContent, item.getContent());
	        values.put(MyDataBaseHelper.DetailImage, item.getImage());
	    
	        db.update(MyDataBaseHelper.Details, values, MyDataBaseHelper.DetailID + " = " + item.getId(), null);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }
    
    // getting single
    public Details getDetails(int id) {
    	try {
	    	Cursor cursor = db.query(MyDataBaseHelper.Details, allColumns, MyDataBaseHelper.DetailID + " = " + id, 
	    			null, null, null, null);
	    	if(cursor != null && cursor.getCount() > 0) {
	    		cursor.moveToFirst();
	    		Details item = cursorToDetails(cursor);
		    	if(cursor != null && !cursor.isClosed())
		    		cursor.close();
		    	return item; 
	    	} else
	    		return null;
    	} catch (Exception e) {
    		e.printStackTrace();
    		return null;
    	}
    }
    
    // getting all
    public List<Details> getAllDetail() {
    	try{
	    	List<Details> list = new ArrayList<Details>();
	    	
	    	String selectQuery = "SELECT  * FROM " + MyDataBaseHelper.Details;
	    	Cursor cursor = db.rawQuery(selectQuery, null);
	    	if(cursor != null && cursor.getCount() > 0) {
		    	if(cursor.moveToFirst()) {
		    		do{
		    			Details item = cursorToDetails(cursor);
		    			list.add(item);
		    		} while(cursor.moveToNext());
		    	}
		    	
		    	if(cursor != null && !cursor.isClosed())
		    		cursor.close();
		    	
		    	return list;
	    	} else
	    		return null;
    	} catch (Exception e) {
    		e.printStackTrace();
    		return null;
    	}
    }
    
    // getting details with category_id
    public List<Details> getChannelByCategories(int categoriesID) {
    	try {
	    	List<Details> list = new ArrayList<Details>();
	    	
	    	Cursor cursor = db.query(MyDataBaseHelper.Details, allColumns, MyDataBaseHelper.DetailCategoriesId  + " = " + categoriesID, 
	    			null, null, null, null);
	    	if(cursor != null && cursor.getCount() > 0 ) {
		    	if(cursor.moveToFirst()) {
		    		do{
		    			Details item = cursorToDetails(cursor);
		    			list.add(item);
		    		} while(cursor.moveToNext());
		    	}
		    	
		    	if(cursor != null && !cursor.isClosed())
		    		cursor.close();
		    	
		    	return list;
	    	} else
	    		return null;
    	} catch (Exception e) {
    		e.printStackTrace();
    		return null;
    	}
    
    }
    
	 // Getting  Count
	 public int getDetailCount() {
		try {
	        String countQuery = "SELECT  * FROM " + MyDataBaseHelper.Details;
	        Cursor cursor = db.rawQuery(countQuery, null);
	        cursor.close();
	 
	        // return count
	        return cursor.getCount();
		} catch (Exception e) {
    		e.printStackTrace();
    		return 0;
		}
    }
    
    // Channel from cursor
    private Details cursorToDetails(Cursor cursor) {
    	Details item = new Details();
    	try {
	    	item.setId(cursor.getInt(0));
	    	item.setCategories_id(cursor.getInt(1));
	    	item.setTitle(cursor.getString(2));
	    	item.setContent(cursor.getString(3));
	    	item.setImage(cursor.getString(4));
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
        return item;
      }

}
